#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"questwidget.h"
#include"citywidget.h"
#include"editwidget.h"
#include"deletewidget.h"
#include"editcityname.h"
#include"addwidget.h"
#include<QErrorMessage>
#include<QDebug>
#include<QFile>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    QFile file2("city.txt");
    //读入交通信息
    if(!file2.open(QFile::ReadOnly|QFile::Text))
    {
        return;
    }
    QTextStream in2(&file2);
    QString line2 = in2.readLine();
    //QStringList r = line2.split(" ");
    while(!line2.isNull()){
        graphmodule.addcity(line2);
        line2 = in2.readLine();
        qDebug()<<line2;
    }
    file2.close();
    QFile file("traffic.txt");
    if(!file.open(QFile::ReadOnly|QFile::Text))
    {
        return;
    }
    QTextStream in(&file);
    QString line = in.readLine();
    while(!line.isNull())
    {
        bool ok;
        QStringList r = line.split(" ");
        QString bcity , lcity;
        bcity = r.at(0);
        lcity = r.at(1);
        date bdate(r.at(2));
        date ldate(r.at(3));
        int cost = r.at(4).toInt(&ok);
        bool type = r.at(5).toInt(&ok);
        graphmodule.addtfinfo(bcity,lcity,bdate,ldate,cost,type);
        line = in.readLine();
    }
    ui->setupUi(this);
    this->setWindowTitle("交通查询系统");
}

MainWindow::~MainWindow()
{
    //写入

    QFile file("traffic.txt");
    if(!file.open(QFile::WriteOnly)){return;}
    QTextStream out(&file);
    for(auto i : graphmodule.citys)
    {
        for(int type = 0; type <= 1;type++)
        {
            for(auto j : i.paths[type])
            {
                out<<j.f->getname()<<" "<<j.e->getname()<<" "<< j.b.to_text() <<" " << j.l.to_text()<<" "
                    << QString::number(j.cost)<<" "<<QString::number((int)type)<<'\n';
            }
        }
    }
    file.close();
    QFile file2("city.txt");
    if(!file2.open(QFile::WriteOnly)){return;}
    QTextStream out2(&file2);
    for(auto i : graphmodule.citys){
        out2<<i.getname()<<'\n';
    }
    delete ui;
}

void MainWindow::on_pushButton_Q_clicked()//查询交通信息
{
    ui->textBrowser->clear();
    Questwidget *dlg=new Questwidget(this)  ;
    dlg->exec();
    QString begincity=dlg->begin;
    QString endcity=dlg->last;
    int st=dlg->s;
    bool type=dlg->type;
    int ans1 = 0;
    date ans2(0,0);
    QVector<Tinfo*>tmp;
    if(st==0)return;
    else if(st==1){
        tmp=graphmodule.solution2(type,begincity,endcity,&ans2,&ans1);
        if(tmp.isEmpty()){
            ui->textBrowser->append("没有找到相应的路径");
            return;
        }
        ui->textBrowser->append("从"+begincity+"到"+endcity+"的最快路径为");
        for(int i=0;i<tmp.size();i++){
            ui->textBrowser->append(tmp[i]->f->getname()+"->"+tmp[i]->e->getname()+" "+tmp[i]->b.to_text()+"-"+tmp[i]->l.to_text());
            ui->textBrowser->append("票价"+QString::number(tmp[i]->cost));
            //qDebug()<<QString::number(tmp[i]->cost);
        }

    }
    else if(st==2){
        tmp= graphmodule.solution1(type,begincity,endcity,&ans1,&ans2);
        if(tmp.isEmpty()){
            ui->textBrowser->append("没有找到相应的路径");
            return;
        }
        ui->textBrowser->append("从"+begincity+"到"+endcity+"的最省钱路径为");
        for(int i=0;i<tmp.size();i++){
            QString x3=tmp[i]->b.to_text();
            QString x4=tmp[i]->l.to_text();
            city * q = tmp[i]->f;
            QString x1 = q->getname();
            city * q2 = tmp[i]->e;
            QString x2 = q2->getname();
            QString x5=QString::number(tmp[i]->cost);
            ui->textBrowser->append(x1+"->"+x2);
            ui->textBrowser->append(x3+"-"+x4);
            ui->textBrowser->append("票价"+QString::number(tmp[i]->cost));
        }
    }
    ui->textBrowser->append("共用时"+ans2.to_text());
    ui->textBrowser->append("花费"+QString::number(ans1)+"元");
    delete(dlg);
}
void MainWindow::on_pushButton_AC_clicked()//增加城市信息
{
    citywidget *dlg=new citywidget(this);
    dlg->exec();
    bool us=dlg->us;
    QString cname=dlg->cityname;
    if(us==0)return;
    else if(us==1){
        if(cname.isEmpty()){
            QErrorMessage *dlg1=new QErrorMessage(this);
            dlg1->setWindowTitle("错误信息");
            dlg1->showMessage("城市名为空！");
            return;
        }
        if(!graphmodule.addcity(cname)){
            QErrorMessage *dlg1=new QErrorMessage(this);
            dlg1->setWindowTitle("错误信息");
            dlg1->showMessage("城市已经存在！");
            return;
        }
    }
    delete(dlg);
}

void MainWindow::on_pushButton_DC_clicked()//删除城市信息
{
    citywidget *dlg=new citywidget(this);
    dlg->exec();
    bool us=dlg->us;
    QString cname=dlg->cityname;
    if(us==0)return;
    else if(us==1){
        if(cname.isEmpty()){
            QErrorMessage *dlg=new QErrorMessage(this);
            dlg->setWindowTitle("错误信息");
            dlg->showMessage("城市名为空！");
            return;
        }
        bool deleted=graphmodule.deletecity(cname);
        if(!deleted){
            QErrorMessage *dlg1=new QErrorMessage(this);
            dlg1->setWindowTitle("错误信息");
            dlg1->showMessage("城市名为空！");
            return;
        }
    }
    delete(dlg);
}


void MainWindow::on_pushButton_ET_clicked()//编辑交通信息
{
    editwidget *dlg=new editwidget(&graphmodule,this);
    dlg->exec();
    delete(dlg);
}


void MainWindow::on_pushButton_DT_clicked()//删除交通信息
{
    Deletewidget *dlg=new Deletewidget(&graphmodule,this);
    dlg->exec();
    delete(dlg);
}

void MainWindow::on_pushButton_EN_clicked()//编辑城市名称
{
    editcityname *dlg=new editcityname(this);
    dlg->exec();
    QString ocn=dlg->ocityname;
    QString ncn=dlg->ncityname;
    graphmodule.change(ocn,ncn);
    delete(dlg);
}

void MainWindow::on_pushButton_AT_clicked()//增加交通信息
{
    Addwidget *dlg=new Addwidget(&graphmodule,this);
    dlg->exec();
    delete(dlg);
}
